﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.Windows.Forms;
using System.Text;
using System.IO;
namespace zmMoniter
{
    public partial class frmServerSetting : DevExpress.XtraEditors.XtraForm
    {
        private mainForm main = null;
        public frmServerSetting(mainForm frm)
        {
            InitializeComponent();
            main = frm;
        }
        public bool reSetData()
        {
            bool bResult = false;

            textEdt_ClientIP.Text = mainForm.mIP;
            textEdt_ClientPort.Text = mainForm.mPort;
            textEdt_ServerIP.Text = mainForm.sIP;
            textEdt_ServerPort.Text = mainForm.sPort;

            bResult = true;
            
            return bResult;
        }
        private void sBtn_TestServer_Click(object sender, EventArgs e)
        {
            
            label_Msg.Text = "正在测试...";
            label_Msg.Visible = true;
            TcpUtilClient client;
            IPEndPoint iep;
            iep = new IPEndPoint(IPAddress.Parse(textEdt_ServerIP.Text), int.Parse(textEdt_ServerPort.Text));
            client = new TcpUtilClient(iep, true, 60000);
            Int32 result = client.Connect();
            if (result == 1)
            {

                label_Msg.Text = "连接成功！";
            }
            else
            {
                label_Msg.Text = "连接失败！";
            }
            Hide();
            
        }
        private void sBtn_Save_Click(object sender, EventArgs e)
        {
           
            mainForm.sIP = textEdt_ServerIP.Text;
            mainForm.sPort = textEdt_ServerPort.Text;
            setStr();
            this.Hide();
        }
        private bool setStr()
        {
            bool bCon = false;
            try
            {
                FileStream aFile = new FileStream(Application.StartupPath + "\\config.ini", FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read);
                StreamWriter sw = new StreamWriter(aFile);
                sw.WriteLine("[CONNSTRING]=" + mainForm.connstring);
                sw.WriteLine("[SIP]=" + mainForm.sIP);
                sw.WriteLine("[SPORT]=" + mainForm.sPort);
                sw.Close();
                bCon = true;
            }
            catch (IOException ex)
            {
                bCon = false;
            }
            return bCon;
        }
        private bool getStr()
        {
            bool bCon = false;
            try
            {
                FileStream aFile = new FileStream(Application.StartupPath + "\\config.ini", FileMode.OpenOrCreate, FileAccess.Read);
                StreamReader sr = new StreamReader(aFile);
                string nextLine;
                while ((nextLine = sr.ReadLine()) != null)
                {
                    if (nextLine.IndexOf("[SIP]") >= 0)
                    {
                        int iPos = nextLine.IndexOf("[SIP]");

                        mainForm.sIP = nextLine.Substring(iPos + 6, nextLine.Length - 6);
                    }
                    else if (nextLine.IndexOf("[SPORT]") >= 0)
                    {
                        int iPos = nextLine.IndexOf("[SPORT]");
                        mainForm.sPort = nextLine.Substring(iPos + 8, nextLine.Length - 8);
                    }
                }
                sr.Close();
                bCon = true;
            }
            catch (IOException ex)
            {
                bCon = false;
            }
            return bCon;
        }
    }
}